import { Component, EventEmitter, Injector, Output, ViewChild, Input, OnInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    StockZT_BaseInfoServiceProxy,
    EditDtoExtensionsItem,
    Stock_BaseFieldListDto
} from '@shared/service-proxies/stock-service-proxies';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { isUndefined } from 'ngx-bootstrap/chronos/utils/type-checks';
import { isNullOrUndefined } from 'util';
import { CodeItemServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { element } from '@angular/core/src/render3';
// import { ToolingZT_BaseInfoServiceProxy, Stock_ToolingBaseFieldListDto } from '@shared/service-proxies/tooling-service-proxies';
@Component({
    selector: 'createOrEditToolingBaseInfoModal',
    templateUrl: './create-or-edit-tooling-baseinfo-modal.component.html'
})
export class CreateOrEditToolingBaseInfoModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _stockZT_BaseInfoServiceProxy: StockZT_BaseInfoServiceProxy,
        private _codeItemServiceProxy: CodeItemServiceProxy
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() stocktype;
    active = false;
    saving = false;
    _type;
    selectedValue = new Stock_BaseFieldListDto();
    editDto;
    extensions: Array<EditDtoExtensionsItem>;
    extendextensions: Array<EditDtoExtensionsItem>;
    optionAll;
    itemcode = '';
    ngOnInit(): void {}
    showinfo(id?: string, type?: string, codeItemId?: string, codeName?: string): void {
        this.active = true;
        this._type = type;
        switch (this._type) {
            case '工装':
                if (id == null || id == undefined) {
                    if (codeItemId == undefined || codeItemId == null) {
                        this.notify.error(this.l('请选择工装编码分类！'));
                        return;
                    }
                    this._codeItemServiceProxy.getCodeItemsForEdit(codeItemId).subscribe(result => {
                        if (result.f_CodeItemId.length != 6) {
                            this.message.warn('请重新选择工装编码分类！');
                            return;
                        } else {
                            this._stockZT_BaseInfoServiceProxy
                                .getToolingDtoExtensionsById(id,result.f_CodeItemId )
                                .subscribe(result2 => {
                                    this.optionAll = {
                                        f_DimensionGroupIDOptions: result2.dimentionSelectOptions,
                                        f_UnitIdOptions: result2.unitIdSelectOptions
                                    };
                                    this.editDto = result2.toolingOutput;
                                    this.extendextensions = result2.extendItems;
                                    // this.extensions = result.baseItems;
                                    this.editDto.f_StockType = this._type;
                                    this.modal.show();
                                });
                        }
                    });
                } else {
                    this._stockZT_BaseInfoServiceProxy.getToolingDtoExtensionsById(id, '').subscribe(result => {
                        this.optionAll = {
                            f_DimensionGroupIDOptions: result.dimentionSelectOptions,
                            f_UnitIdOptions: result.unitIdSelectOptions
                        };
                        this.editDto = result.toolingOutput;
                        this.extendextensions = result.extendItems;
                        // this.extensions = result.baseItems;
                        this.editDto.f_StockType = this._type;
                        this.modal.show();
                    });
                }

                break;
        }
    }

    save(): void {
        this.saving = true;
        let t: object = _.filter(this.optionAll.f_DimensionGroupIDOptions, {
            value: this.editDto.f_DimensionGroupID
        });

        this.editDto.f_DimensionGroup = t[0].label;

        switch (this._type) {
            case '工装':
                this._stockZT_BaseInfoServiceProxy
                    .createOrUpdateBITooling(this.editDto)
                    .pipe(
                        finalize(() => {
                            this.saving = false;
                        })
                    )
                    .subscribe(() => {
                        this.notify.info(this.l('SavedSuccessfully'));
                        this.close();
                        this.modalSave.emit(null);
                    });
                break;
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
